package com.stx.blog.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.stx.blog.dao.BlogDao;
import com.stx.blog.entity.BlogEntity;
import com.stx.blog.vo.BlogVo;
import com.sun.script.javascript.JSAdapter;

public  class BlogDaoImpl extends JdbcDaoSupport implements BlogDao{
  @Override
	public List<BlogEntity> list(BlogVo vo){
		String sql ="select * from blog order by bid desc";
		JdbcTemplate jt=getJdbcTemplate();
		List<BlogEntity> blogList=jt.query(sql,new BlogRowMapper());
		return blogList;
	}
	
	class BlogRowMapper implements RowMapper{
		public Object mapRow(ResultSet rs,int rowNum) throws SQLException{
			BlogEntity entity=new BlogEntity();
			
			entity.setBid(rs.getInt("bid"));
			entity.setBtitle(rs.getString("btitle"));
			entity.setBcontent(rs.getString("bcontent"));
			entity.setBcreateTime(rs.getDate("bcreateTime"));
			return entity;
			
		}
		
	}



	@Override
	public void addBlog(BlogVo blog) {
		
		JdbcTemplate jt=getJdbcTemplate();
		String sql="insert into blog(bid,btitle,bcontent,categoryId) values(blog_seq.nextval,?,?,?)";
		Object[] args={blog.getBtitle(),blog.getBcontent(),blog.getCategoryId()};
		jt.update(sql,args);
	}

	@Override
	public void delBlog(Integer bid) {
		
		JdbcTemplate jt=getJdbcTemplate();
		String sql="delete from blog where bid=?";
		Object[] args={bid};
		jt.update(sql,args);
	}

	@Override
	public void updateBlog(BlogVo blog) {
		
		JdbcTemplate jt=getJdbcTemplate();
		String sql="update blog set btitle=?,bcontent=?";
		Object[] args ={blog.getBtitle(),blog.getBcontent()};
		jt.update(sql,args);
	}

	@Override
	public BlogEntity BlogListByBid(Integer bid) {
		JdbcTemplate jt=getJdbcTemplate();
		String sql="select *from blog where bid=?";
		Object[] args={bid};
		BlogEntity blogEntity=(BlogEntity)jt.queryForObject(sql, args,new BlogRowMapper());
		return blogEntity;
	}
}
